package com.iemes.controller.metals;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;





import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MetalsBomFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.metals.MetalsBomMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/metals")
@Controller
public class MetalsBomController extends BaseController {

	@Inject
	private MetalsBomMapper metalsBomMapper;
	
	@RequestMapping("metalsbom")
	public String metalsBom(Model model){
		List<MetalsBomFormMap> metalsBomFormMap=metalsBomMapper.findByWhere(new MetalsBomFormMap());
		model.addAttribute("metalsbom", metalsBomFormMap);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/metals/bom/metalsbom_list";
	}
	
	@ResponseBody
	@RequestMapping("metalsbom_findByPage")
	public PageView findByPage(String pageNow, String pageSize) 
	{
		MetalsBomFormMap metalsFormMap = getFormMap(MetalsBomFormMap.class);
		metalsFormMap.put("state", 0);
		metalsFormMap=toFormMap(metalsFormMap, pageNow, pageSize,metalsFormMap.getStr("orderby"));
		List<MetalsBomFormMap> metalsFormMaps = metalsBomMapper.findByPage(metalsFormMap);
		
		for(int i=0;i<metalsFormMaps.size();i++){
			if(metalsFormMaps.get(i).getInt("oldForNewService")==0){
				metalsFormMaps.get(i).put("oldForNewService", "否");
			}else{
				metalsFormMaps.get(i).put("oldForNewService", "是");
			}
		}
		
		pageView.setRecords(metalsFormMaps);
		return pageView;
	}
	
	
	@RequestMapping("metalsbom_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		//获取物料对象
		
		MetalsBomFormMap metalsBomFormMap = metalsBomMapper.findbyFrist("id", id, MetalsBomFormMap.class);
		
			
		model.addAttribute("metalsbom", metalsBomFormMap);
		

		return Common.BACKGROUND_PATH + "/metals/bom/metalsbom_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="metalsbom_editEntity")
	@SystemLog(module="五金物料清单",methods="物料-修改物料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		MetalsBomFormMap metalsBomFormMap = getFormMap(MetalsBomFormMap.class);
		metalsBomMapper.editEntity(metalsBomFormMap);
		return "success";
	}
	
	
	@ResponseBody
	@RequestMapping(value="metalsbom_deleteEntity")
	@SystemLog(module="五金物料清单",methods="物料-删除物料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		
		MetalsBomFormMap metalsBomFormMap=getFormMap(MetalsBomFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		
		for(int i=0;i<str.length;i++){
			System.out.println("循环打印"+str[i]+"呱呱");
			metalsBomFormMap.set("state", 1);
			metalsBomFormMap.put("id", str[i]);
			metalsBomMapper.editEntity(metalsBomFormMap);
		}
		
		return "success";
	}
	
	
	@RequestMapping("metalsbom_addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/metals/bom/metalsbom_add";
	}
	
	
	@ResponseBody
	@RequestMapping("metalsbom_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="五金物料清单",methods="物料-新增物料")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		MetalsBomFormMap metalsBomFormMap = getFormMap(MetalsBomFormMap.class);
		// 获取request
		//HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		//UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		//metalsBomFormMap.set("create_time", new Date());
		System.out.println("鸡头江"+new Date());
		java.sql.Date date=new java.sql.Date(new Date().getTime());
		System.out.println("我"+date);
		metalsBomFormMap.set("create_time", date);
		//metalsBomFormMap.put("byuser_id", userFormMap.getInt("id"));
		try {
			metalsBomMapper.addEntity(metalsBomFormMap);
		} catch (Exception e) {
			// TODO: handle exception
			return "false";
		}
		return "success";
	}
	
	
}
